﻿namespace PMS.Client.FrmImportAndExport
{
    using AsyncFactory;
    using PMS.BLL;
    using PMS.Client.FrmOrgan;
    using PMS.Common;
    using PMS.Model;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class FrmUserDataImport : Form
    {
        private Button btnCancel;
        private Button btnGetUserData;
        private Button btnParentID;
        private Button btnSave;
        private IContainer components = null;
        private Label label1;
        private Label label2;
        private int OrganID = 0;
        private TextBox ParentID;
        private TextBox UserData;
        private string userDataPath = "";

        public FrmUserDataImport()
        {
            this.InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnGetUserData_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Filter = "数据备份|*.xml";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    this.userDataPath = dialog.FileName;
                    this.UserData.Text = dialog.FileName;
                }
            }
        }

        private void btnParentID_Click(object sender, EventArgs e)
        {
            FrmSelect select = new FrmSelect();
            select.ShowDialog();
            if (select.DialogResult == DialogResult.OK)
            {
                this.ParentID.Text = select.FullPathName;
                this.OrganID = select.Organ.OrganID;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool flag;
            if (this.OrganID == 0)
            {
                MessageBox.Show("请选择导入目的机构！");
            }
            else if (this.userDataPath == "")
            {
                MessageBox.Show("请选择导入用户数据！");
            }
            else
            {
                flag = false;
                AsyncProcess.Process(delegate {
                    try
                    {
                        OrganExportAndImport importData = (OrganExportAndImport) SerializationHelper.Load(typeof(OrganExportAndImport), this.userDataPath);
                        flag = new OrganImportBLL().UserDataImport(this.OrganID, importData);
                    }
                    catch (Exception exception)
                    {
                        Log.Write(exception.Message);
                        flag = false;
                    }
                }, delegate {
                    if (flag)
                    {
                        MessageBox.Show("用户数据导入成功！");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("用户数据导入失败，数据已回滚！");
                    }
                }, true, LoadingImageType.Strip);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmUserDataImport));
            this.label1 = new Label();
            this.btnParentID = new Button();
            this.ParentID = new TextBox();
            this.btnGetUserData = new Button();
            this.UserData = new TextBox();
            this.label2 = new Label();
            this.btnCancel = new Button();
            this.btnSave = new Button();
            base.SuspendLayout();
            this.label1.AutoSize = true;
            this.label1.Location = new Point(0x1f, 15);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x71, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "请选择导入目的机构";
            this.btnParentID.BackColor = Color.Gainsboro;
            this.btnParentID.FlatAppearance.BorderColor = Color.DimGray;
            this.btnParentID.FlatStyle = FlatStyle.Flat;
            this.btnParentID.Location = new Point(0x286, 11);
            this.btnParentID.Name = "btnParentID";
            this.btnParentID.Size = new Size(0x15, 0x15);
            this.btnParentID.TabIndex = 0x36;
            this.btnParentID.Text = "…";
            this.btnParentID.UseVisualStyleBackColor = false;
            this.btnParentID.Click += new EventHandler(this.btnParentID_Click);
            this.ParentID.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.ParentID.BorderStyle = BorderStyle.FixedSingle;
            this.ParentID.Location = new Point(150, 11);
            this.ParentID.Name = "ParentID";
            this.ParentID.ReadOnly = true;
            this.ParentID.Size = new Size(0x205, 0x15);
            this.ParentID.TabIndex = 0x35;
            this.btnGetUserData.BackColor = Color.Gainsboro;
            this.btnGetUserData.FlatAppearance.BorderColor = Color.DimGray;
            this.btnGetUserData.FlatStyle = FlatStyle.Flat;
            this.btnGetUserData.Location = new Point(0x286, 0x26);
            this.btnGetUserData.Name = "btnGetUserData";
            this.btnGetUserData.Size = new Size(0x15, 0x15);
            this.btnGetUserData.TabIndex = 0x39;
            this.btnGetUserData.Text = "…";
            this.btnGetUserData.UseVisualStyleBackColor = false;
            this.btnGetUserData.Click += new EventHandler(this.btnGetUserData_Click);
            this.UserData.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.UserData.BorderStyle = BorderStyle.FixedSingle;
            this.UserData.Location = new Point(150, 0x26);
            this.UserData.Name = "UserData";
            this.UserData.ReadOnly = true;
            this.UserData.Size = new Size(0x205, 0x15);
            this.UserData.TabIndex = 0x38;
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x1f, 0x2a);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x71, 12);
            this.label2.TabIndex = 0x37;
            this.label2.Text = "请选择待导入的数据";
            this.btnCancel.BackColor = Color.White;
            this.btnCancel.FlatStyle = FlatStyle.Popup;
            this.btnCancel.Location = new Point(0x1b7, 0x56);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 0x3b;
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = false;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnSave.BackColor = Color.White;
            this.btnSave.FlatStyle = FlatStyle.Popup;
            this.btnSave.Location = new Point(0xfd, 0x56);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new Size(0x4b, 0x17);
            this.btnSave.TabIndex = 0x3a;
            this.btnSave.Text = "导入";
            this.btnSave.UseVisualStyleBackColor = false;
            this.btnSave.Click += new EventHandler(this.btnSave_Click);
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            base.ClientSize = new Size(0x2c3, 0x7c);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnSave);
            base.Controls.Add(this.btnGetUserData);
            base.Controls.Add(this.UserData);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.btnParentID);
            base.Controls.Add(this.ParentID);
            base.Controls.Add(this.label1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.Name = "FrmUserDataImport";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "用户数据导入";
            base.ResumeLayout(false);
            base.PerformLayout();
        }
    }
}

